from database import Base
from sqlalchemy import Column, String, Integer, Float, DateTime, ForeignKey
from sqlalchemy.orm import relationship
import datetime


# 导入数据库基类
# 导入常用字段,类型,外键
# 导入关系

# 作者表
class Author(Base):
    __tablename__ = "author"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(32), index=True)
    email = Column(String(32))
    # relation反向查找,这里相当于通过author查book,所以这里是用class name "Book",而不是表名,这是orm的功能
    author_to_book = relationship("Book", backref="book_to_author")


class Book(Base):
    __tablename__ = "book"
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(32))
    price = Column(Float)
    publish_date = Column(DateTime, default=datetime.datetime.now())
    # 外键关联
    auth_id = Column(Integer, ForeignKey("author.id"))


class Publish(Base):
    __tablename__ = "publish"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(32))
    publish_to_book = relationship("Book", backref="book_to_publish", secondary="match")



class Match(Base):
    __tablename__ = "match"
    id = Column(Integer, primary_key=True, index=True)
    publish_id = Column(Integer, ForeignKey("publish.id"))
    book_id = Column(Integer, ForeignKey("book.id"))

